// Tags:
#include <cctype>
#include <cstdio>
#include <cstring>

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

const int N = 100, dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int n, m, ans = 0x3f3f3f3f;
char map[N][N];
bool tmp[N][N];

void solve(int step) {
  char tmp2[N][N];
  memcpy(tmp2, tmp, sizeof(tmp2));
  for (int k = 0; k < 4; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        if (tmp[i][j] == '1') tmp[i + dx[k]][j + dy[k]] = '1';
      }
    }
  }
  memcpy(tmp, tmp2, sizeof(tmp));
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("flow.in", "r", stdin);
  freopen("flow.out", "w", stdout);
#endif
#endif

  read(n), read(m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", map[i] + 1);
    for (int j = 1; j <= m; ++j) {
      if (map[i][j] == '1') tmp[i][j] = true;
    }
  }
  solve(1);
  printf("%d", ans);
  return 0;
}